package com.ls.fw.data.search.impl.dao;

import java.io.Serializable;

import org.apache.solr.client.solrj.SolrServer;
import org.springframework.data.solr.core.SolrTemplate;

import com.ls.fw.data.search.impl.dao.data.SolrDataDao;
import com.ls.fw.data.search.impl.dao.data.SolrDataDaoImpl;
import com.ls.fw.data.utils.DataUtil;

@SuppressWarnings("unchecked")
public abstract class EntitySearchDaoAdapter<M, PK extends Serializable> extends SearchDaoImpl implements EntitySearchDao<M, PK>{

	/**
	 * 实体类类型(由机关办法主动赋值)
	 */
	protected Class<M> entityClass = null;
	
	protected Class<PK> pkClass = null;
	
	/**
	 * Sping data实现的基础操作
	 */
	protected SolrDataDao<M,PK> dataDao = null;
	
	
	
	public EntitySearchDaoAdapter(String address, boolean autoCommit) {
		super(address, autoCommit);
	}

	public EntitySearchDaoAdapter(String name, SolrServer server,
			SolrTemplate template) {
		super(name, server, template);
	}

	public EntitySearchDaoAdapter(String address) {
		super(address);
	}

	public EntitySearchDaoAdapter() {
		super();
		init();
	}
 
	public EntitySearchDaoAdapter(SolrServer server) {
		super(server);
		init();
	}
	
	private void init(){
		if(entityClass == null){
			this.entityClass = DataUtil.getGenerricType(this.getClass());
		}
		if(this.entityClass!=null && this.dataDao==null && this.template!=null){
			dataDao = new SolrDataDaoImpl<M,PK>(this.template,this.entityClass);
		}
	}
	 

	@Override
	public boolean open(Object _address) {
		boolean flag = super.open(_address);
		init();
		return flag;
	}


	public Class<M> getEntityClass() {
		return this.entityClass;
	}


	public void setEntityClass(Class<M> entityClass) {
		this.entityClass = entityClass;
		init();
	}
  
	/**
	 * 设置实体信息
	 * 
	 * @author lisheng
	 * @date 2014年12月31日 下午7:01:46
	 * @version V1.0
	 * @param entityClass
	 * @param pkClass
	 */
	public EntitySearchDaoAdapter<M, PK> setEntityInfo(Class<M> entityClass,Class<PK> pkClass){
		this.entityClass = entityClass;
		this.pkClass = pkClass;
		init();
		return this;
	}


	@Override
	public SolrDataDao<M, PK> getDataDao() {
		return dataDao;
	}


	@Override
	public void setServer(SolrServer server) {
		super.setServer(server);
	}


	@Override
	public void setTemplate(SolrTemplate template) {
		super.setTemplate(template);
		this.init();
	}
	
	
}
